package com.example.demo.dao;


import com.example.demo.Enity.Activity;
import com.example.demo.Enity.privateTalk;
import com.example.demo.Enity.friendsList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.ArrayList;

public interface ChatJpa extends JpaRepository<privateTalk, Integer>, JpaSpecificationExecutor<privateTalk> {
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "insert into private_talk(sender,to_user,message) value(?1,?2,?3)")
    void addPrivateTalk(String sender,String toUser,String message);

    @Query(nativeQuery = true, value = "select * from private_talk P where (P.sender = ?1 or P.sender = ?2) and (P.to_user=?1 or P.to_user=?2) and P.sender !=P.to_user order by P.id")
    ArrayList<privateTalk> findChathistory(String sender,String toUser);

    @Query(nativeQuery = true, value = "select * from private_talk P where (P.sender = ?1 or P.sender = ?2) and (P.to_user=?1 or P.to_user=?2)  order by P.id")
    ArrayList<privateTalk> findChathistory2(String sender,String toUser);

    @Query(nativeQuery = true, value = "select P.id,P.mineid,P.otherid,P.message,U.yb_userhead,U.yb_usernick from private_list P join user U on ((P.mineid = U.yb_userid and P.otherid=?1) or (P.otherid = U.yb_userid and P.mineid = ?1)) where (P.mineid = ?1 or P.otherid=?1)")

    ArrayList findFriendsList(String sender);

}
